/**
 * 
 */
package com.blog.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.blog.pojo.SysMenu;
import com.blog.pojo.SysRole;
import com.blog.service.SysMenuService;
import com.blog.service.SysRoleMenuService;
import com.blog.service.SysRoleService;
 
@Controller
@RequestMapping("config")
public class RoleController {
    @Autowired SysRoleService sysRoleService;
    @Autowired SysRoleMenuService sysRoleMenuService;
    @Autowired SysMenuService sysMenuService;
     
    @RequestMapping("listRole")
    public String list(Model model){
        List<SysRole> rs= sysRoleService.list();
        model.addAttribute("rs", rs);
         
        Map<SysRole,List<SysMenu>> role_permissions = new HashMap<>();
         
        for (SysRole role : rs) {
            List<SysMenu> ps = sysMenuService.list(role);
            role_permissions.put(role, ps);
        }
        model.addAttribute("role_permissions", role_permissions);
 
        return "listRole";
    }
    @RequestMapping("editRole")
    public String list(Model model,long id){
        SysRole role =sysRoleService.get(id);
        model.addAttribute("role", role);
         
        List<SysMenu> ps = sysMenuService.list();
        model.addAttribute("ps", ps);
 
        List<SysMenu> currentPermissions = sysMenuService.list(role);
        model.addAttribute("currentPermissions", currentPermissions);
         
        return "editRole";
    }
    @RequestMapping("updateRole")
    public String update(SysRole role,long[] permissionIds){
        sysRoleMenuService.setMenus(role, permissionIds);
        sysRoleService.update(role);
        return "redirect:listRole";
    }
 
    @RequestMapping("addRole")
    public String list(Model model,SysRole role){
        System.out.println(role.getName());
        System.out.println(role.getRemark());
        sysRoleService.add(role);
        return "redirect:listRole";
    }
    @RequestMapping("deleteRole")
    public String delete(Model model,long id){
        sysRoleService.delete(id);
        return "redirect:listRole";
    }   
 
}
